package com.hoo.controller.account;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hoo.common.constant.Constant;
import com.hoo.entity.order.SearchOrderInfoResult;
import com.hoo.entity.order.Ticket;
import com.hoo.entity.order.TicketOrder;
import com.hoo.form.order.TicketForm;
import com.hoo.form.order.TicketOrderForm;
import com.hoo.service.order.TicketOrderService;
import com.hoo.util.PaginationSupport;

@Controller
@RequestMapping(value="/order/")
public class OrderController extends BaseController{

	@Autowired
	TicketOrderService ticketOrderService;
	
	@RequestMapping(value="search_order",method=RequestMethod.GET)
	public String searchOrder(){
		return "order/searchOrder";
	}
	
	@RequestMapping(value="search_order",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> doSearchOrder(TicketOrderForm form,HttpServletRequest request){
		 PaginationSupport<TicketOrder> ps = this.ticketOrderService.searchTicketOrder(form);
	     return dataTableJson(ps.getTotalCount(), ps.getItems());
	}
	
	@RequestMapping(value="search_order_info",method=RequestMethod.GET)
	public String searchOrderInfo(){
		return "order/searchOrderInfo";
	}
	
	@RequestMapping(value="search_order_info",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> doSearchOrderInfo(TicketOrderForm form){
		 PaginationSupport<SearchOrderInfoResult> ps = this.ticketOrderService.searchTicketOrderInfo(form);
	     return dataTableJson(ps.getTotalCount(), ps.getItems());
	}
	
	
	@RequestMapping(value="manage_ticket",method=RequestMethod.GET)
	public String searchTicket(){
		return "ticket/manageTicket";
	}
	
	@RequestMapping(value="search_ticket",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> doSearchTicket(TicketForm form){
		 PaginationSupport<Ticket> ps = this.ticketOrderService.searchTicketOrderInfo(form);
	     return dataTableJson(ps.getTotalCount(), ps.getItems());
	}
	
	
	@RequestMapping(value = "/update_ticket/{type}/{ticketNo}", method = RequestMethod.GET)
	public String udpateTicket(@PathVariable("type") String type,@PathVariable("ticketNo") String ticketNo, Model model, HttpServletRequest request) {
		if("update".equals(type)){
			model.addAttribute("ticket", this.ticketOrderService.getTicketByTicketNo(ticketNo));
		}else {
			type="add";
		}
		model.addAttribute("operationType", type);
		return "ticket/updateTicket";
	}
	
	@RequestMapping(value = "/update_ticket/{operationType}", method = RequestMethod.POST)
	@ResponseBody
	public String doUdpateTicket(TicketForm form ,@PathVariable("operationType") String operationType ,Model model, HttpServletRequest request) {
		try {
			if("update".equals(operationType)){
				if(form.getStatus()==1){
					this.ticketOrderService.updateAllTicket(2);
				}
				return this.ticketOrderService.updateTicket(form)==1?Constant.SUCCESS:Constant.FAIL;
			}else if("add".equals(operationType)){
				return this.ticketOrderService.insertTicket(form)==1?Constant.SUCCESS:Constant.FAIL;
			}
		} catch (Exception e) {
		}
		return "fail";
	}
	
	@RequestMapping(value = "/check_ticket_status", method = RequestMethod.POST)
	@ResponseBody
	public String checkTicketStatus(HttpServletRequest request){
		try {
			if(null != this.ticketOrderService.getTicketOnUse()){
				return "existence";
			}
		} catch (Exception e) {
		}
		return "uexistence";
	}
}

